package com.dayuanit.dy8.mall.springbootmall.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class PageController {

    @RequestMapping(value = {"/", "/toHome"})
    public String toHome() {
        return "home/home";
    }

    @RequestMapping("/toCommonInfo/{commonId}")
    public String toCommonInfo(@PathVariable("commonId") int commonId, HttpServletRequest request) {
        request.setAttribute("commonId", "commonId");
        return "home/introduction";
    }

    @RequestMapping(value = "/pay/toPay/{commoId}/{buyNum}")
    public String toPay(@PathVariable("commoId") int commoId, @PathVariable("buyNum") int buyNum,
                        HttpServletRequest request,
                        HttpSession session) {
        session.setAttribute("commoId", commoId);
        session.setAttribute("buyNum", buyNum);
        return "home/pay";
    }

    @RequestMapping("/user/toAddress")
    public String toAddress(HttpServletResponse response) {
        final Cookie cookie = new Cookie("username", "tom");
        cookie.setPath("/");
        cookie.setMaxAge(60);
        cookie.setHttpOnly(true);
        response.addCookie(cookie);
        return "person/address";
    }

    @RequestMapping("/user/toCart")
    public String toCart(HttpServletResponse response, HttpServletRequest request, @RequestParam("id") String[] ids) {
//        System.out.println(">>>>>>>>" +  request.getParameter("id"));
//
//        String[] ids = request.getParameterValues("id");
//        for (String id : ids) {
//            System.out.println(id);
//        }

//        >>>>>>>>1
//        1
//        2

        for (String id : ids) {
            System.out.println(id);
        }


        return "home/shopcart";
    }

    @RequestMapping("/user/toCart2")
    public String toCart2(HttpServletResponse response, HttpServletRequest request, @RequestParam("id") List<String> ids) {

        for (String id : ids) {
            System.out.println(id);
        }

        return "home/shopcart";
    }
}
